package com.qf.j2205.controller;

import com.alibaba.fastjson.JSON;
import com.qf.j2205.entity.Screenings;
import com.qf.j2205.entity.vo.ScreeningsVo;
import com.qf.j2205.service.ScreeningsService;
import com.qf.j2205.service.impl.ScreeningsServiceImpl;
import com.qf.j2205.util.SrcUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ScreeningsServlet
 * @Description TODO
 * @Author Windy
 * @Date 2022/8/15 19:39
 * @Version 1.0
 **/
public class ScreeningsServlet extends ServletBase {


    /**
     * 添加新的场次信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void insertSrc(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String openDate = req.getParameter("input0");
            Date date = new SimpleDateFormat("yyyy-MM-dd").parse(openDate);
            int maxPerson = Integer.parseInt(req.getParameter("txt_num").isEmpty() ? "100" : req.getParameter("txt_num"));
            int netOrder = Integer.parseInt(req.getParameter("txtOrderNumber").isEmpty() ? "100" : req.getParameter("txtOrderNumber"));
            String openText = req.getParameter("TextBox3");
            Date openTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(openText);
            String closeText = req.getParameter("TextBoxclose");
            Date closeTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(closeText);
            Screenings screenings = new Screenings(0,date, maxPerson, netOrder, openTime, closeTime, "未审核");
            ScreeningsService service = new ScreeningsServiceImpl();
            boolean b = service.insertScreenings(screenings);
            System.out.println(b);
            if (b){
                resp.sendRedirect("/manage/Manage.html");
            }else {
                resp.sendRedirect("../error.html");
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询全部场次信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void selectSrc(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ScreeningsService service = new ScreeningsServiceImpl();
        List<Screenings> list = service.selectScreenings();
        List<ScreeningsVo> vos = SrcUtil.ScreeningsToVos(list);

        if (vos!=null) {
            String json = JSON.toJSONString(vos);
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.println(json);
            writer.flush();
            writer.close();
        }else {
            resp.sendRedirect("../error.html");
        }
    }

    /**
     * 删除
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteSrc(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println(req.getParameter("scid"));

        int acid = Integer.parseInt(req.getParameter("scid"));

        ScreeningsService service = new ScreeningsServiceImpl();
        Screenings oneById = service.findOneById(acid);
        boolean b = service.deleteScreenings(acid);
        if (b){

            String json = JSON.toJSONString(oneById);
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.println(json);
            writer.flush();
            writer.close();
        }else {
            resp.sendRedirect("../error.html");
        }
    }

    /**
     * 审核
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void checkSrc(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println(req.getParameter("scid"));
        int acid = Integer.parseInt(req.getParameter("scid"));

        ScreeningsService service = new ScreeningsServiceImpl();
        boolean b = service.checkScreenings(acid);
        String json=null;
        if (b){
             json = JSON.toJSONString("success");

        }else {
             json = JSON.toJSONString("fail");
        }
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.println(json);
        writer.flush();
        writer.close();
    }

    /**
     * 根据id查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findOneSrc(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println(req.getParameter("scid"));
        int acid = Integer.parseInt(req.getParameter("scid"));

        ScreeningsService service = new ScreeningsServiceImpl();
        Screenings one = service.findOneById(acid);
        ScreeningsVo vo = SrcUtil.ScreeningsToVo(one);
        if (vo != null) {
            String json = JSON.toJSONString(vo);
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.println(json);
            writer.flush();
            writer.close();
        }else {
            resp.sendRedirect("../error.html");
        }
    }

    /**
     * 更新数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateSrc(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            int acid = Integer.parseInt(req.getParameter("scid"));
            String openDate = req.getParameter("input0");
            Date date = new SimpleDateFormat("yyyy-MM-dd").parse(openDate);
            int maxPerson = Integer.parseInt(req.getParameter("txt_num").isEmpty() ? "100" : req.getParameter("txt_num"));
            int netOrder = Integer.parseInt(req.getParameter("txtOrderNumber").isEmpty() ? "100" : req.getParameter("txtOrderNumber"));
            String openText = req.getParameter("TextBox3");
            Date openTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((openDate+" "+openText));
            String closeText = req.getParameter("TextBoxclose");
            Date closeTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((openDate+" "+closeText));
            String isad = req.getParameter("DropDownList1");

            Screenings screenings = new Screenings(acid,date, maxPerson, netOrder, openTime, closeTime, isad);
            ScreeningsService service = new ScreeningsServiceImpl();
            boolean b = service.updateScreenings(screenings);
            if (b){
                resp.sendRedirect("/Manage.html");
            }else {
                resp.sendRedirect("../error.html");
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 条件查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void selectTimeAndIsaudit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] str={"未审核","已审核",null};
        int isaudit = Integer.parseInt(req.getParameter("isaudit"));
        System.out.println(req.getParameter("time"));
        String time = req.getParameter("time").isEmpty() ? null : req.getParameter("time");
        ScreeningsService service = new ScreeningsServiceImpl();
        System.out.println("time"+time+"\n isaudit"+str[isaudit]);
        List<Screenings> screenings = service.selectTimeAndIsaudit(time, str[isaudit]);

        List<ScreeningsVo> vos = SrcUtil.ScreeningsToVos(screenings);
        System.out.println(vos);
        if (vos != null) {
            String json = JSON.toJSONString(vos);
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.println(json);
            writer.flush();
            writer.close();
        }else {
            resp.sendRedirect("../error.html");
        }
    }

    /**
     * 审核全部选中的数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void detectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String det = req.getParameter("det");
        ScreeningsService service = new ScreeningsServiceImpl();
        boolean b = service.detectAll(det);
        if (b){
            String json = JSON.toJSONString("success");
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.println(json);
            writer.flush();
            writer.close();
        }else {
            resp.sendRedirect("error.html");
        }
    }
}
